package com.gsj.lx.mybatislx.controller;

import com.gsj.lx.mybatislx.config.SqlUtils;
import com.gsj.lx.mybatislx.dao.UserService;
import com.gsj.lx.mybatislx.mapper.UserMapper;
import com.gsj.lx.mybatislx.model.MyUser;
import com.gsj.lx.mybatislx.model.User;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: swaggerToWrodlx
 * @description:
 * @author: 郭书江
 * @create: 2022-05-26 11:03
 **/
@Slf4j
@RestController
@RequestMapping("myuser")
@Api(tags = "myuser接口")
public class MyUserController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;

    /**
     * 动态sql拼接查询
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/get")
    public List<MyUser> get() throws Exception {

        List<MyUser> myUserAll = userService.getMyUserAll();
        log.info("查询"+myUserAll);
        return myUserAll;
    }

    /**
     * 动态sql条件查询
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/getWhere")
    public List<MyUser> getWhere() throws Exception {
        MyUser myUser = new MyUser();
        myUser.setId(1);
        myUser.setName("1");
        String sql = SqlUtils.selectseek(myUser);
        List<MyUser> myUserAll = userMapper.getMyUserAll(sql);
        log.info("查询"+myUserAll);
        return myUserAll;
    }

    /**
     * 动态sql插入
     * @param myUser
     * @return
     * @throws Exception
     */
    //todo id主键自增的情况下可为空,其他属性不可为空,拼接不了,待完善
    @PostMapping(value = "/insertOne")
    public Integer insertOne(@RequestBody MyUser myUser) throws Exception {
        String sql = SqlUtils.insert(myUser);
        int insertsql = userMapper.insertsql(sql);
        log.info("成功插入[%s]"+insertsql);
        return insertsql;
    }
    /**
     * 动态sql批量插入
     *
     * @return
     * @throws Exception
     */
    //todo
    @PostMapping(value = "/insertAll")
    public Integer insertAll(@RequestBody List<MyUser> myUser) throws Exception {
        String sql = SqlUtils.insertAll(myUser);
        int insertsql = userMapper.insertsql(sql);
        log.info("成功插入[%s]"+insertsql);
        return insertsql;
    }

    /**
     * 动态sql更新
     *
     * @return
     * @throws Exception
     */
    //todo id主键自增的情况下可为空,其他属性不可为空,拼接不了,待完善
    @PostMapping(value = "/updateOne")
    public Integer updateOne(@RequestBody MyUser myUser) throws Exception {
        /*MyUser myUser = new MyUser();
        myUser.setName("1");
        myUser.setAge(1);
        myUser.setEmail("@");*/
        String sql = SqlUtils.insert(myUser);
        int insertsql = userMapper.insertsql(sql);
        log.info("成功插入[%s]"+insertsql);
        return insertsql;
    }


}
